//
//  AppHomeController.m
//  gungunchou
//
//  Created by lyzbmacos3 on 2018/12/4.
//  Copyright © 2018年 xskj. All rights reserved.
//

#import "AppHomeController.h"
#import "BannerCell.h"
#import "OtherClassCell.h"
#import "GoodsCell.h"
#import "HomeIndexModel.h"
#import "AppGoodsDetailController.h"
#import "HomeViewController.h"
#import "WebViewController.h"

#import "OrignalWebController.h"

#import "AppWebController.h"

#import "LeftCateView.h"

@interface AppHomeController () <UICollectionViewDelegate, UICollectionViewDataSource, OtherClassCellClickDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectView;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewTop;

@property (nonatomic, strong) LeftCateView *cateView;

@property (nonatomic, strong) HomeIndexModel *homeModel;
@end

@implementation AppHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];//因为初始加载launchScreen隐藏了,这里重新显示出来
    [self.collectView registerNib:[UINib nibWithNibName:@"BannerCell" bundle:nil] forCellWithReuseIdentifier:@"BannerCell"];
    [self.collectView registerNib:[UINib nibWithNibName:@"OtherClassCell" bundle:nil] forCellWithReuseIdentifier:@"OtherClassCell"];
    [self.collectView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsCell"];
    
    [self httpGetHomeData];
    
    if ([LJUtility isIphoneX]) {
        self.topViewTop.constant += 24;
    }
    
    [self initLeftCateView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

#pragma mark - 网络请求
- (void)httpGetHomeData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[CGNetManage shared] httpGetHomeDataWithCompleted:^(NSDictionary *dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.homeModel = [HomeIndexModel mj_objectWithKeyValues:dic];
        [self.collectView reloadData];
    } fail:^(NSError *err) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.dataList.count;
    if (section == 2) {
        return self.homeModel.ProductList.count;
    }
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        BannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BannerCell" forIndexPath:indexPath];
        cell.model = self.homeModel;
        
        return cell;
    }
    
    if (indexPath.section == 1) {
        OtherClassCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OtherClassCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    
    ProductModel *productModel = [self.homeModel.ProductList objectAtIndex:indexPath.row];
    GoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCell" forIndexPath:indexPath];
    cell.productModel = productModel;
    
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2) {
        ProductModel *productModel = [self.homeModel.ProductList objectAtIndex:indexPath.row];
        
//        NSString *url = [NSString stringWithFormat:@"%@/%@",GoodsDetail_Url_1,productModel.ID];
//        [self jumpHomeWebController:url];
        
        AppGoodsDetailController *con = AppGoodsDetailController.new;
        con.model = productModel;
        con.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:con animated:YES];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH,SCREEN_HEIGHT*0.25);
    }
    else if (section == 1) {
        return CGSizeMake(SCREEN_WIDTH, 100);
    }
    
    
    return CGSizeMake((SCREEN_WIDTH-10)/2.0,240);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 2) {
        return UIEdgeInsetsMake(10,0,10,0);
    }
    return UIEdgeInsetsMake(0,0,0,0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 2) {
        return 10;
    }
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (section == 2) {
        return 10;
    }
    return 0;
}



- (IBAction)cateClick:(id)sender {
    self.cateView.hidden = NO;
}

- (IBAction)searchClick:(id)sender {
    
}

- (IBAction)carClick:(id)sender {
    if (!Usr_Is_L) {
        [LICGToast showWithText:@"请先登录!"];
    }
    else [self jumpHomeWebController:Usr_Is_L?CarList_Url_2:CarList_Url_1];
}

- (void)initLeftCateView
{
    self.cateView = [[LeftCateView alloc] init];
    
    WS(weakSelf)
//    @"主页",@"我的订单",@"积分",@"收货地址",@"个人中心",@"积分抽奖",@"公司介绍"
    self.cateView.clickBlock = ^(NSInteger index) {
        if (index == 1) {
            weakSelf.tabBarController.selectedIndex = 0;
        }
        else if (index == 2) {
            weakSelf.tabBarController.selectedIndex = 2;
        }
        else if (index == 3) {
            [weakSelf jumpHomeWebController:Usr_Is_L?Integral_Url_2:Integral_Url_1];
        }
        else if (index == 4) {
            [weakSelf jumpHomeWebController:Usr_Is_L?RecAddress_Url_2:RecAddress_Url_1];
        }
        else if (index == 5) {
            weakSelf.tabBarController.selectedIndex = 3;
        }
        else if (index == 6) {
            [weakSelf jumpHomeWebController:Usr_Is_L?ScroeGet_Url_2:ScroeGet_Url_1];
        }
        else if (index == 7) {
            [weakSelf jumpHomeWebController:CompanyIntroduce_Url_1];
        }
        
        if (index != 0) {
            weakSelf.cateView.hidden = YES;
        }
    };
    [self.view addSubview:self.cateView];
    [self.cateView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView);
        make.left.bottom.right.equalTo(self.view);
    }];
    self.cateView.hidden = YES;
}

- (void)jumpHomeWebController:(NSString *)url
{
//    HomeViewController *home = [[HomeViewController alloc] init];
//    home.theLoadUrl = url;
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:home];
//    [self.navigationController showViewController:home sender:nil];
    
//    WebViewController *web = [[WebViewController alloc] init];
//    web.lastWeb = self.wkWeb;
//    web.homeWeb = self.homeWeb;
//    web.UrlString = url;
//    [self.navigationController pushViewController:web animated:YES];

    
//    WebViewController *con = WebViewController.new;
    AppWebController *con = AppWebController.new;
    con.UrlString = url;
    con.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:con animated:YES];
    
    /*
    OrignalWebController *web = OrignalWebController.new;
    web.loadUrl = url;
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
     */
}


#pragma mark - cell delegate
- (void)OtherClassCell_ItemClickWithIndex:(NSInteger)index
{
    if (index == 0) {//商品列表
        self.tabBarController.selectedIndex = 1;
    }
    else if (index == 1) {//积分
        [self jumpHomeWebController:Usr_Is_L?Integral_Url_2:Integral_Url_1];
    }
    else if (index == 2) {//我的订单
        self.tabBarController.selectedIndex = 2;
    }
    else if (index == 3) {//我的
        self.tabBarController.selectedIndex = 3;
    }
}
@end
